<?php

namespace HelloAdmin\Controller;

// 后台用户模块
class UserController extends CommonController {

    private $User;

    public function __construct() {
        parent::__construct();
        $this->User = D("User");
    }

    function _filter(&$map) {
        $map['id'] = array('egt', 2);
        $map['account'] = array('like', "%" . $_POST['account'] . "%");
    }
    
    public function index() {
        $where = '1 = 1';
        $post = $this->GetParam('username');
        if (!empty($post)) {
            $where .= ' AND `username` = ' . "'{$post}'";
        }

        $count = $this->User->where($where)->count('user_id');
        parent::backPage($count);
        $data = $this->User->where($where)->limit($this->offset, $this->numPerPage)->select();

        $this->assign('list', $data);

        $this->display();
    }

    public function password() {

        $this->display();
    }

    public function foreverdelete() {
        if($_GET['user_id'] == 1){
            $this->error('操作被禁用！');
        }
        if ($_GET['user_id'] > 0) {
            if ($this->User->where("`user_id` = '{$_GET['user_id']}'")->delete()) {
                $this->success('删除成功');
            }
        }
        //var_dump($this->User->getLastSql());exit;
        $this->error('删除失败');
    }

    // 检查帐号
    public function checkAccount($username, $t) {
        if (!preg_match('/^[a-z]\w{4,}$/i', $username)) {
            $this->error('用户名必须是字母，且5位以上！');
        }
        // 检测用户名是否冲突
        $result = $this->User->checkRepeat($username);
        if ($result) {
            $this->error('该用户名已经存在！');
        } else {
            if ($t != 'add') {
                $this->success('该用户名可以使用！');
            }
        }
    }

    // 插入数据
    public function insert() {
        if ($_POST['user_id'] > 0) {
            $data = $this->GetParam('user_id,username,remark,disabled,email');
        } else {
            // 创建数据对象
            $data = $this->GetParam('username,remark,password,disabled,email');
            $this->checkAccount($data['username'], 'add');
            $pass_account = getRandStr(10);
            $password = genSecretStr($data['password'], $pass_account);
            $data_ = array(
                'pass_account' => $pass_account,
                'password' => $password,
                'last_time' => time(),
                'createtime' => time()
            );
            $data = array_merge($data, $data_);
        }
        //var_dump($data);//exit;
        //var_dump($this->User);exit;
        //var_dump($this->Userfields);exit;
        // 写入帐号数据
        if ($result = $this->User->SaveData($data)) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    protected function addRole($userId) {
        //新增用户自动加入相应权限组
        $RoleUser = M("RoleUser");
        $RoleUser->user_id = $userId;
        // 默认加入网站编辑组
        $RoleUser->role_id = 3;
        $RoleUser->add();
    }

    //重置密码
    public function resetPwd() {
        $id = $_POST['user_id'];
        $password = $_POST['password'];
        if ('' == trim($password) || empty($id)) {
            $this->error('密码不能为空！');
        }
        $pass_account = getRandStr(10);
        $password = genSecretStr($password, $pass_account);
        $this->User->password = md5($password);
        $data = array('user_id' => $id, 'password' => $password, 'pass_account' => $pass_account);
        $result = $this->User->SaveData($data);
        if (false !== $result) {
            //echo $this->User->getLastSql();
            $this->success("密码修改为:" . $_POST['password']);
        } else {
            //echo $this->User->getLastSql();
            $this->error('重置密码失败！');
        }
    }

}

?>